const express = require("express");
const router = express.Router();
const servAdmin = require("../sever/serveAdmin");

const {asynchandler} =  require("../router/getResult");
const cry = require('../util/crypt')
const servBook = require("../sever/serveBook");
const servUser = require("../sever/serveUser");
router.get("/getuser", asynchandler(async(req,res)=>{
    console.log(req.adminId);
    if(!req.adminId){
        return null;
    }
   
    const page = req.query.page || 1 ;
    const limit = req.query.limit || 10;
    const result =  await servUser.getAllUser(page,limit);

    return result;

}))

router.get("/getUserId",asynchandler(async(req,res)=>{
    if(!req.adminId){
        return null;
    }
    const id = req.query.id ;

    const result = await servUser.getUserId(id);
    return result;

}))
router.put("/upUser",asynchandler(async(req,res)=>{
    if(!req.adminId){
        return null;
    }

    const id = req.body.id ;
    const obj = req.body.obj;

    const result = await servUser.updataUser(obj,id);


    return result;


}))

router.delete("/deleteUser",asynchandler(async(req,res)=>{
    const isadmin = await  servAdmin.getAdmin(req.adminId);
    if(isadmin){
        const result = await servUser.deleteUser(req.query.id)
        return  result; 
    }else{
        return null;
    }
    
}))

router.post('/login',asynchandler(async (req,res)=>{
    
    const loginId = req.body.loginId;
    const paw = req.body.paw;
    const result = await servAdmin.login(loginId,paw);

    if(result.id){
      const cryId = result.id
      result.id =  cry.encrypto(cryId)
      const value = cry.encrypto(cryId);
      res.cookie("adminId",value,{
        path:"/",
        maxAge:'3600000'
      })
      res.header("adminId",value);
      return result;

    }else{
        return null;
    }




}))


router.get("/getBooks",asynchandler(async(req,res)=>{
    if(!req.adminId){
        return null;
    }
    const page = req.query.page || 1;
    const limit = req.query.limit || 10;
    const result = await servBook.getBooks(page,limit);

    return result;
}))

router.delete("/deleBook",asynchandler(async(req,res)=>{
    const isadmin = await  servAdmin.getAdmin(req.adminId);
    if(isadmin){
        const result = await servBook.deleBook(req.query.id)
        return  result; 
    }else{
        return null;
    }
}))


router.put("/upBook",asynchandler(async(req,res)=>{
    if(!req.adminId){
        return null;
    }
    const id = req.body.id ;
    const obj = req.body.obj;
    const result = await servBook.updataBook(id,obj);
    return result;


}))

router.put("/addBook",asynchandler(async(req,res)=>{
    if(!req.adminId){
        return null;
    }
    console.log(res);
    const  result = await servBook.addBook(req.body.book);

    return result
}))
router.get("/getBookId",asynchandler(async(req,res)=>{
    if(!req.adminId){
        return null;
    }
    const id = req.query.id ;

    const result = await servBook.findBook(id);
    return result;

}))

module.exports = router;

